package org.example.leetcpde;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

public class roman {
    @Test
    void intToRoman() {
        System.out.println(intToRoman(68));
    }

    int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

    public String intToRoman(int num) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            int value = values[i];

            while (num >= value) {
                num -= value;
                builder.append(symbols[i]);
            }
        }

        return builder.toString();
    }



    int romanToInt(String s) {
        Map<Character, Integer> map = new HashMap<>() {{
            put('I', 1);
            put('V', 5);
            put('X', 10);
            put('L', 50);
            put('C', 100);
            put('D', 500);
            put('M', 1000);
        }};

        char[] chars = s.toCharArray();
        int len = chars.length, sum = 0;
        for (int i = 0; i < len; i++) {
            int v = map.get(chars[i]);
            if (i <= len - 2 && v < map.get(chars[i + 1])) {
                sum -= v;
            } else {
                sum += v;
            }
        }

        return sum;
    }
}
